import path from 'path';

import nodeExternals from 'webpack-node-externals';

import getLoaders from './loaders';
import { getPlugins } from './plugins';

// import getenv from 'getenv';
const Dir = global.DIR;

// import {clientPath,serverPath,distPath,resolve} from '../config';

import vendor from '@/config/vendor';
const clientPath = path.join(Dir.browser, "index.js");
const serverPath = "../server/server.prod.js";
const distPath = "../../.dist";

const getBaseConfig = (env) => {
	return {
		devtool: env === 'production' ? 'source-map' : 'cheap-module-eval-source-map',
		target: 'web',
		plugins: getPlugins(env, false),
		module: {
			rules: getLoaders(env)
		},
		resolve: {
			extensions: ['.web.js','.js', '.jsx', 'json'],
		}
	};
};

export const getDevConfig = () => {
	return Object.assign({}, getBaseConfig('development'), {
		entry: {
			index: [
				'react-hot-loader/patch',
				'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=10000&reload=true',
				Dir.browser
			],
			vendor: vendor
		},
		output: {
			path: "/",
			publicPath: '/',
			filename: '[name].[hash:8].js', //文件名,不加chunkhash,以方便调试时使用，生产环境下可以设置为 [name].bundle.[hash:8].js
			chunkFilename: 'chunk.[name].[hash:8].js', //块文件索引 chunk.[name].[hash:8].js
			sourceMapFilename: "[name].map.js", //映射名
		}
	});
};
export const getProdConfigs = () => {
	const env = 'production';
	const config = Object.assign({}, getBaseConfig(env), {
		entry: {
			index: [clientPath],
			vendor: vendor
		},
		output: {
			path: distPath + '/brower',
			publicPath: '/public',
			filename: '[name].[hash:8].js', //文件名,不加chunkhash,以方便调试时使用，生产环境下可以设置为 [name].bundle.[hash:8].js
			chunkFilename: 'chunk.[hash:8].[name].js', //块文件索引 chunk.[name].[hash:8].js

		}
	});
	const serverConfig = Object.assign({}, config, {
		entry: {
			serverRender: [serverPath]
		},
		output: Object.assign({}, config.output, {
			path: distPath + '/server',
			filename: '[name].js',
			libraryTarget: 'commonjs2'
		}),
		plugins: getPlugins(env, true),
		target: 'node',
		externals: [nodeExternals()]
	});
	return [config, serverConfig];
};
export default getProdConfigs();
